package com.zimmur.service.impl.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.ConstData;
import com.zimmur.dao.goods.IGoodsOrganizationDao;
import com.zimmur.model.base.AdBanner;
import com.zimmur.model.goods.GoodsOrganization;
import com.zimmur.model.goods.view.GoodsOrganizationView;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.goods.IGoodsOrganizationService;
import com.zimmur.tools.PageUtil;

@Service("goodsOrganizationService")
public class GoodsOrganizationServiceImpl implements IGoodsOrganizationService {
	private static Logger log=LoggerFactory.getLogger(GoodsOrganizationServiceImpl.class);
	@Resource
	private IGoodsOrganizationDao goodsOrganizationDao;
	@Resource
	private IAdBannerService adBannerService;
	
	//缓存所需类
	@Resource
	private RedisCacheUtil<GoodsOrganizationView> stringRedisDao;
	private final static String REDIS_KEY ="goodsOrganizationService";
	private static Object syncLock = new Object();
	private static Object count_syncLock = new Object();
	
	@Override
	public PageUtil<GoodsOrganizationView> selectIndexPage(int pageNo,String keyword) {
		String key = REDIS_KEY + "_" + pageNo+"_"+keyword+"_selectIndexPage";
		String count_key = REDIS_KEY + "_"+keyword+"_selectIndexPage_count";
		PageUtil<GoodsOrganizationView> page=new PageUtil<>();
		List<GoodsOrganizationView> organizationViewList=null;
		if(pageNo==0){
			pageNo=1;
		}
		Integer total=0;
		boolean countExist = false;
		try {
			countExist = stringRedisDao.exists(count_key);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if(countExist){
			//从缓存中读取
			total=Integer.valueOf(stringRedisDao.getCacheString(count_key));
		}else{
			synchronized(count_syncLock){
				try {
					countExist = stringRedisDao.exists(count_key);
				} catch (Exception e) {
					log.error(e.getMessage());
				}
				if(countExist){
					//从缓存中读取
					total=Integer.valueOf(stringRedisDao.getCacheString(count_key));
				}else{
					total=goodsOrganizationDao.countOrganizationViewByPage(keyword);
					try {
						//存入缓存
						stringRedisDao.setCacheString(count_key, total+"");
						stringRedisDao.setTimeout(count_key, ConstData.REDIS_KEY_TIMEOUT_2);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
				}
			}
		}
		page.setPageNo(pageNo);
		page.setTotalRecord(total);
		
		try {
			//是否已存在
			boolean bRedis = false;
			try {
				bRedis = stringRedisDao.exists(key);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				organizationViewList = stringRedisDao.getCacheList(key);
			}else{
				synchronized (syncLock) {
					//二次判断  优化代码锁
					try {
						bRedis = stringRedisDao.exists(key);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						organizationViewList = stringRedisDao.getCacheList(key);
					}else{
						
						//缓存中不存在，创建后存入
						//从数据库读取
						organizationViewList = goodsOrganizationDao.selectOrganizationViewByPage(page.getPageBeginNo(), page.getPageSize(),keyword,null);
						try {
							//存入缓存
							stringRedisDao.setCacheList(key,organizationViewList);
							stringRedisDao.setTimeout(key, ConstData.REDIS_KEY_TIMEOUT_2);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
			if(organizationViewList!=null && organizationViewList.size()>0){				
				page.setItemList(organizationViewList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}



	@Override
	public Map<String, Object> selectIndex() {
		Map<String, Object> map=new HashMap<>();
		PageUtil< GoodsOrganizationView> goodsView=selectIndexPage(1,null);
		map.put("organizationList",goodsView);
		List<AdBanner> adBannerList = adBannerService.selectAdList(Integer.parseInt(AppModuleEnum.Organization.getModuleId()));
		if(adBannerList!=null && adBannerList.size() > 0){
			map.put("adBannerList", adBannerList);
		}else{
			map.put("adBannerList", new ArrayList<>());
		}
		return map;
	}



	@Override
	public GoodsOrganization selectByGoodsId(long goodsId) {
		
		return goodsOrganizationDao.selectByGoodsId(goodsId);
	}



	@Override
	public List<GoodsOrganizationView> selectRecommend(int num) {
		String key = REDIS_KEY + "_" + num+"_selectRecommend";
		List<GoodsOrganizationView> organizationViewList=null;
		try {
			//是否已存在
			boolean bRedis = false;
			try {
				bRedis = stringRedisDao.exists(key);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				organizationViewList = stringRedisDao.getCacheList(key);
			}else{
				synchronized (syncLock) {
					//二次判断
					try {
						bRedis = stringRedisDao.exists(key);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						organizationViewList = stringRedisDao.getCacheList(key);
					}else{
						//缓存中不存在，创建后存入
						//从数据库读取
						organizationViewList = goodsOrganizationDao.selectOrganizationViewByPage(0, num,null,1);
						try {
							//存入缓存
							stringRedisDao.setCacheList(key,organizationViewList);
							stringRedisDao.setTimeout(key, ConstData.REDIS_KEY_TIMEOUT_2);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return organizationViewList;
	}



	@Override
	public int updateOrganizationByKey(GoodsOrganization goodsOrganization) {
		int flag=0;
		try {
			flag = goodsOrganizationDao.updateByPrimaryKeySelective(goodsOrganization);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
		
		return flag;
	}



	@Override
	public List<GoodsOrganization> selectGoodsOrganizatonByTime(int unixTime) {
		List<GoodsOrganization> list=goodsOrganizationDao.selectByTime(unixTime);
		return list;
	}

}
